
// import { config } from 'vue/types/umd';
import router from './router'
import store from './store'
const axios = require('axios').default;


export default function setaxios() {

  //request的拦截
  axios.interceptors.request.use(config => {
    ''("config",config);
    if (store.state.token) {
      config.headers.token = store.state.token;
    }
    return config
  })

  //每次的请求返回都要经过这里
  axios.interceptors.response.use(response=>{
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    //状态码为200说明登录成功，返回参数
    if(response.status==200){
      var data=response.data;
      //账号过期，就直接清空历史记录，并返回登录页面
      if(data.code==-1){
        this.$store.commit('setaxios','');
        window.localStorage.removeItem('token');
        router.replace("/login");
      }
      return data;

    }
     
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  })


}


